public with sharing class HCPChangeExt {
    String choice = null;
    public String msg{get;set;}
    String hcpId = System.currentPageReference().getParameters().get('id');
   String flag = null;
    List<HCP__c> target = [select Name, approvalstatus__c,class1__c, class2__c, class3__c, approvaltype__c, Active__c, changetoclass1__c, changetoclass2__c, classchangeto3__c, comments__c from HCP__c where Id =: System.currentPageReference().getParameters().get('id')];
    public boolean showbutton{get;set;}
    
    public void settrue(){
        showbutton=true;
    }
    
    public List<SelectOption> getItems() {List<SelectOption> options = new List<SelectOption>();
        //options.add(new SelectOption('ACTIVATE', '新增职业医护人员'));
        options.add(new SelectOption('DELETE','删除医护人员')); 
        options.add(new SelectOption('CLASS','改变医护人员级别')); 
        return options; 
    }
    
    public HCP__c hcp {
        get{
            Boolean flag1 = false;
            if (hcp==null)
                if(target.size() > 0)
                    hcp = target[0];
                else
                    hcp=null;
            return hcp;
        }
        set;
    }
 
    public PageReference Submit() {
        Boolean flag2 = false;
           if(hcp.approvalstatus__c=='审批通过但未生效'||hcp.approvalstatus__c=='等待审批'){
        msg = '您还有尚未生效的审批申请，生效前不可再次提交审批。';
        showbutton=false;
        return null;
    } 
    try{
    
        if (hcp.Active__c){
       //     system.assert(false,choice == 'CLASS' && hcp.class3__c == hcp.classchangeto3__c && hcp.class1__c == hcp.changetoclass1__c && hcp.class2__c == hcp.changetoclass2__c);
            if(choice == 'CLASS' && hcp.class3__c == hcp.classchangeto3__c && hcp.class1__c == hcp.changetoclass1__c && hcp.class2__c == hcp.changetoclass2__c){
                hcp.adderror('改变级别失败：级别未改变');
            }
            if(choice == 'CLASS' && hcp.classchangeto3__c==null && hcp.changetoclass1__c==null && hcp.changetoclass2__c==null ){
                hcp.adderror('改变级别失败：目标级别全为空');
            }
            //else if(choice == 'ACTIVATE')
                //hcp.addError('激活失败：该职业医护人员已激活');
            else{
                update hcp;
                
                // Create an approval request for the HCP__c  
                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                req1.setObjectId(hcpId);
                //req1.setNextApproverIds(new Id[] {hcp.approver__c});
                // Submit the approval request for the HCP__c   
                Approval.ProcessResult result = Approval.process(req1);
                
                return new ApexPages.StandardController(new HCP__c(Id = hcpId)).view();
            }
        }
        else if(hcp.Active__c == false){                           //&& hcp.approvaltype__c == '新增医护人员'
                update hcp;
                
                // Create an approval request for the HCP__c  
                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                req1.setObjectId(hcpId);
                //req1.setNextApproverIds(new Id[] {hcp.approver__c});
                // Submit the approval request for the HCP__c   
                Approval.ProcessResult result = Approval.process(req1);
                
                return new ApexPages.StandardController(new HCP__c(Id = hcpId)).view();
        }
        else if(choice == 'DELETE' || choice == 'CLASS')
            hcp.addError('操作失败：该医护人员已被删除或未被激活');
        return null;}
        catch(Exception e){
        //system.assert(false,e);
            msg='您没有可提交审批的经理，请联系管理员确认经理信息。';
            hcp.approvalstatus__c=null;
            hcp.comments__c=null;
            hcp.changetoclass1__c=null;
            hcp.changetoclass2__c=null;
            hcp.classchangeto3__c=null;
            hcp.approvaltype__c=null;
            update hcp;
            return null;
        }
    }
    
    public PageReference Cancel() {
        Boolean flag2 = false;
        return new PageReference('/a0Y/o'); 
       // return new ApexPages.StandardController(new HCP__c(Id = hcpId)).view();
    }
    
    public PageReference next() {
        //if(choice == 'ACTIVATE'){
            //hcp.approvaltype__c = '新增职业医护人员';
        //  return Page.HCPAdd;
        //}else 
        if(hcp==null){
            msg='对不起，由于网络原因您本次操作失败，请重新提交审批。';
            showbutton=false;
            return null;
        }
        Boolean flag3 = false;
       if(hcp.approvalstatus__c == '审批通过但未生效'||hcp.approvalstatus__c=='等待审批'){
                 msg = '您还有尚未生效的审批申请，生效前不可再次提交审批。';
                return null;
            }
            
        if(choice == 'CLASS'){
            hcp.approvaltype__c = '改变医护人员级别';
            return Page.HCPChangeClass;
        }else if(choice == 'DELETE'){
            hcp.approvaltype__c = '删除医护人员';
            return Page.HCPDelete;
        }
        return null;
    }
    
    public String getChoice() {
        return choice;
    }
      
    public void setChoice(String choice) { this.choice = choice; }
    
  
   
    static testMethod void HCPChangeExtTest()
    {
        HCPChangeExt test = new HCPChangeExt();
        String cho = 'a';
        
        Hospital__c hos = new Hospital__c();
        hos.Name = 'aaaaaaaa';
        hos.Nickname__c = 'aaaaaaaaaaa';
        hos.Active__c = true;
        insert hos;
        
        HCP__c add = new HCP__c();
        add .class1__c = 'NKOL';
        add .class2__c = 'DM';
        add .Hospital__c = hos.Id;
        add .Active__c = true;
        insert add ;        
             
        add.Active__c = true;
        test.target.add(add);
        test.getItems();



        
        test.choice = 'CLASS';
        test.hcp.class3__c = test.hcp.classchangeto3__c;
        test.hcp.class1__c = test.hcp.changetoclass1__c;
        test.hcp.class2__c = test.hcp.changetoclass2__c;
       
        test.Submit();
        
        
        test.hcp.approvalstatus__c = '审批通过但未生效';
        test.Submit();
        
        test.hcp.Active__c = false;
        test.choice = 'DELETE';
        test.hcp.approvaltype__c = 'none';
        test.Submit();      
    
        test.Cancel();
        test.next();
        test.getChoice();
        test.setChoice(cho);
        
        test.choice = 'CLASS';
        test.next();
        
        test.choice = 'none';
        test.next();
        
        test.hcp.approvalstatus__c = '';
        test.next();
        
        test.choice = 'DELETE';
        test.next();
        
        test.choice = 'CLASS';
        test.next();
        
    }

}